﻿using System;
using System.Collections.Generic;
using System.Text;
using SAServices.Model;
using System.Runtime.InteropServices;
using System.IO;
using SAServices.RemtInvtor;

namespace SAServices.Service
{
    public class InvServiceMethod
    {
        public static string lastErrMsg;
        private static string INVOBJ = "Inventor.Application";
        private static Inventor.Application m_InvApp;

        private static void ClearInventorProcess()
        {
            System.Diagnostics.Process[] inventors = System.Diagnostics.Process.GetProcessesByName(@"Inventor");
            foreach (System.Diagnostics.Process inv in inventors)
            {
                inv.Kill();
            }

            m_InvApp = null;
        }

        public static bool IsInventorReady()
        {
            //Inventor.Application app = SAServices.Service.InvServiceMethod.GetInventorApp();
            //bool bRet = app.Ready;
            //return bRet;
            InvtRmtClient clientAction = new InvtRmtClient("222.73.205.208", "90");


            return clientAction.IsInventorReady();
        }

        private static Inventor.Application GetInventorApp()
        {
            //try to get if not exist.
            if (m_InvApp == null)
            {
                try
                {   //try to get the object.

                    m_InvApp = (Inventor.Application)Marshal.GetActiveObject(INVOBJ);
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    lastErrMsg = ex.Message;
                    //File.AppendAllText(@"E:\GlobalModules\01_WCFService\11.log", "invApp=" + lastErrMsg);
                    m_InvApp = null;
                }
                catch (Exception ex)
                {
                    lastErrMsg = ex.Message;
                    //File.AppendAllText(@"E:\GlobalModules\01_WCFService\11.log", "invApp=" + lastErrMsg);
                    m_InvApp = null;
                }
            }
            else
            {
                try
                {
                    bool bReady = m_InvApp.Ready;

                    if (!bReady)
                    {
                        ClearInventorProcess();
                    }
                }
                catch (System.Exception)
                {
                    ClearInventorProcess();
                }
            }

            //try to create if not exist.
            if (m_InvApp == null)
            {
                try
                {
                    System.Type InvType = System.Type.GetTypeFromProgID(INVOBJ);
                    m_InvApp = (Inventor.Application)System.Activator.CreateInstance(InvType);
                }
                catch (Exception ex)
                {
                    lastErrMsg = ex.Message;
                    m_InvApp = null;
                }
            }

            return m_InvApp;
        }

        internal static List<ParameterInfo> GetParameters(string modelPath)
        {

            InvtRmtClient clientAction = null;
            try
            {
                clientAction = new InvtRmtClient("222.73.205.208", "90");


                List<ParameterInfo> infos = clientAction.GetParameters(modelPath);

                clientAction.DisConnection();

                return infos;
                //fill the result list.

            }
            catch (Exception ex)
            {
                lastErrMsg = ex.Message;

                if (clientAction!=null)
                {
                    clientAction.DisConnection();
                }
                //File.AppendAllText(@"E:\GlobalModules\01_WCFService\11.log", "invApp=" + lastErrMsg);
                return null;
            }

        }

        internal static bool GeneratePart(string templatePath, List<ParameterInfo> driveParams, string targetPath)
        {
            InvtRmtClient clientAction = null;

            try
            {
                clientAction = new InvtRmtClient("222.73.205.208", "90");


                bool blRet = clientAction.GeneratePart(templatePath, driveParams, targetPath);

                clientAction.DisConnection();

                return blRet;

            }
            catch (Exception ex)
            {
                lastErrMsg = ex.Message;

                if (clientAction != null)
                {
                    clientAction.DisConnection();
                }

                return false;
            }
        }

        internal static bool DeleteCache(Guid familyId)
        {
            InvtRmtClient clientAction = null;

            try
            {
                clientAction = new InvtRmtClient("222.73.205.208", "90");


                bool blRet= clientAction.DeleteCache(familyId);

                clientAction.DisConnection();

                return blRet;

            }
            catch (Exception ex)
            {
                lastErrMsg = ex.Message;

                if (clientAction != null)
                {
                    clientAction.DisConnection();
                }
                return false;
            }
        }

        internal static bool GenerateCache(string templatePath, List<ParameterInfo> driveParams, List<string> cachePaths)
        {
            InvtRmtClient clientAction = null;

            try
            {

                clientAction = new InvtRmtClient("222.73.205.208", "90");


                bool blRet = clientAction.GenerateCache(templatePath, driveParams, cachePaths);

                clientAction.DisConnection();

                return blRet;
            }
            catch (Exception ex)
            {
                lastErrMsg = ex.Message;

                if (clientAction != null)
                {
                    clientAction.DisConnection();
                }
                return false;
            }

        }

        private static bool CreateDirByPathIfNotExist(string path)
        {
            try
            {
                string dir = System.IO.Path.GetDirectoryName(path);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
    }
}
